/*
 * @Author: wangtianyu wangtianyu@lierda.com
 * @Date: 2024-12-25 14:05:13
 * @LastEditors: wangtianyu wangtianyu@lierda.com
 * @LastEditTime: 2025-03-14 17:54:18
 * @FilePath: \kake_mcu_tool\Application\i2c_device.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: wangtianyu wangtianyu@lierda.com
 * @Date: 2024-12-25 14:05:13
 * @LastEditors: wangtianyu wangtianyu@lierda.com
 * @LastEditTime: 2025-03-10 17:51:04
 * @FilePath: \kake_mcu_tool\Application\i2c_device.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "i2c_device.h"
#include "soft_i2c.h"

#include "stdio.h"
#include "string.h"


void soft_i2c_pin_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Pin = CH7_Pin;
	HAL_GPIO_Init(CH7_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH8_Pin;
	HAL_GPIO_Init(CH8_GPIO_Port, &GPIO_InitStruct);
}

void soft_i2c_pin_deinit(void)
{
	HAL_GPIO_DeInit(CH7_GPIO_Port, CH7_Pin);
	HAL_GPIO_DeInit(CH8_GPIO_Port, CH8_Pin);
}



void i2c_func(kkui_menu_event_e event, char* tab_name, void* param)
{
    if(event == ENTER_TAG)
	{
        soft_i2c_pin_init();
        soft_i2c_init();
    }

    if(event == BACK_LIST)
    {
        soft_i2c_pin_deinit();
    }
}


void i2c_scan_func(kkui_menu_event_e event, char* tab_name, void* param)
{
    uint8_t scan_data = 0;
    uint8_t scan_num = 0;
    char buf[16] = {0};

    if(event == ENTER_TAG)
	{
		uiDisClear(0);
        for(int i = 0; i < 128; i++)
        {
            if(soft_i2c_read_register(i, 0, &scan_data, 1) == 0) // 成功
            {
                memset(buf, 0, 16);
                sprintf(buf, "%02X", i);
                uiShowString(scan_num / 6 * 16, (scan_num % 6) * 10, buf, 8, 0);
                scan_num ++;
            }
        }
        if(scan_num == 0)
        {
            uiShowString(0, 0, "No device found", 8, 0);
        }
    }

    if(event == BACK_LIST)
    {
        ;
    }

    if(event == LOOP_RUN)
    {
		if(uiKeyGetValue(UI_KEY_BACK_ID) == DOWN)
			uiBackAppLoop();
    }
}


void vkl060_demo_func(kkui_menu_event_e event, char* tab_name, void* param)
{
    uint8_t init_cmd[] = {0xc8, 0xbc};
    uint8_t dis_data[] = {0x0b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
    if(event == ENTER_TAG)
	{
        soft_i2c_read_register(0x3e, 0xea, init_cmd, 2); // 成功
    }

    if(event == BACK_LIST)
    {
        ;
    }

    if(event == LOOP_RUN)
    {
		if(uiKeyGetValue(UI_KEY_ENTER_ID) == DOWN)
        {
            soft_i2c_read_register(0x3e, 0xc8, init_cmd, 2); // 成功
        }
        if(uiKeyGetValue(UI_KEY_RIGHT_ID) == DOWN || uiKeyGetValue(UI_KEY_LEFT_ID) == DOWN)
        {
            soft_i2c_read_register(0x3e, 0xc8, dis_data, 7); // 成功
        }
        if(uiKeyGetValue(UI_KEY_BACK_ID) == DOWN)
            uiBackAppLoop();
    }
}

